package whu.cn.whushare.ui.main.my;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import java.io.File;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.ImageWorkerManager;
import whu.cn.whushare.common.UserManager;
import whu.cn.whushare.net.DownloadParams;
import whu.cn.whushare.net.Downloader;
import whu.cn.whushare.ui.common.BaseActivity;
import whu.cn.whushare.ui.main.MainTabActivity;
import whu.cn.whushare.util.EncrypAES;
import whu.cn.whushare.util.Tools;
import whu.cn.whushare.util.Utils;

/**
 * Created by xiaoping on 2014/12/24.
 */
@ViewMapping(id = R.layout.activity_my_info)
public class MyInfoActivity extends BaseActivity implements View.OnClickListener{
    @ViewMapping(id = R.id.avatar_img_change)
    private RelativeLayout mAvatarImgChange;
    @ViewMapping(id=R.id.my_info_avatar_img)
    private ImageView mAvatar;
    @ViewMapping(id = R.id.my_info_nick_name_var)
    private TextView mNickNameChange;
    @ViewMapping(id = R.id.my_info_user_var)
    private TextView mUserChange;
    @ViewMapping(id = R.id.my_info_title_var)
    private TextView mTitleChange;
    @ViewMapping(id = R.id.title_change)
    private RelativeLayout TitleChange;
    @ViewMapping(id = R.id.my_info_phone_var)
    private TextView mPhoneChange;
    @ViewMapping(id = R.id.my_info_sign_var)
    private TextView mSignChange;
    @ViewMapping(id = R.id.my_info_sex_var)
    private TextView mSexChange;

    private UserManager mUserManager;
   // private User mUser;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViews();
        initActions();
    }



    private void initViews() {
        View root = ViewMappingUtil.mapView(this, this);
        getTopBar(root);

        mTopBar.title.setText(R.string.my_info_total);
        mTopBar.searchView.setVisibility(View.INVISIBLE);
        mTopBar.rightImg.setVisibility(View.INVISIBLE);
        Downloader.getInstance(this).getUserInfo(this, null);

        }




    @Override
    public <T> void onSuccess(DownloadParams.PARSING_TYPE type, T response, Object obj) {
        super.onSuccess(type, response, obj);
        switch (type) {
            case USER_INFO_URL:

               User mUser = (User) response;

                if (!TextUtils.isEmpty(mUser.getAvatar())) {
                    ImageWorkerManager.getInstance(this).loadCommonImage(Utils.getPhotoUrl(mUser.getAvatar()), mAvatar,
                            R.drawable.btn_my, R.drawable.btn_my, mAvatar.getWidth(), mAvatar.getHeight());}
                if(mUser.getNickName()!=null)
                mNickNameChange.setText(mUser.getNickName());
                else
                mNickNameChange.setText("未填写");
                if(mUser.getGender()!=null)
                    {
                        if(mUser.getGender().equals("1"))
                           mSexChange.setText("男");
                        else
                           mSexChange.setText("女");
                    }
                else
                mSexChange.setText("未填写");
                if(mUser.getEmail()!=null)
                mUserChange.setText(mUser.getEmail());
                else
                mUserChange.setText("未填写");
                if(mUser.getTitle()!=null)
                    mTitleChange.setText(mUser.getTitle());
                else
                    mTitleChange.setText("未填写");
                if(mUser.getPhone()!=null)
                    mPhoneChange.setText(mUser.getPhone());
                else
                    mPhoneChange.setText("未填写");



                break;
            case LOGIN_URL:
                mUser = (User) response;
                mUser.setPassword(EncrypAES.getInstance().DecryptorToString(mUser.getPassword()));
                mUserManager.setCurrentUser(mUser);
                startActivity(MainTabActivity.getStartIntent(this));
                break;

                default:
                 break;
        }
    }


    private void initActions() {

        mUserManager = UserManager.getInstance(this);
        mAvatarImgChange.setOnClickListener(this);
        mNickNameChange.setOnClickListener(this);
         mTopBar.goBack.setOnClickListener(this);

        TitleChange.setOnClickListener(this);
        mPhoneChange.setOnClickListener(this);
        mSignChange.setOnClickListener(this);
        mSexChange.setOnClickListener(this);

    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.go_back_btn:
                User user=mUserManager.getCurrentUser();
                Downloader.getInstance(this).login(user.getId(),user.getPassword(),1,this,user.getPassword());

                break;
            case R.id.avatar_img_change:
                  startActivity(PictureActivity.getStartIntent(this));
                 break;
            case R.id.my_info_nick_name_var:
                startActivity(InfoNicknameChangeActivity.getStartIntent(this));
                break;

            case R.id.title_change:
                startActivity(InfoTitleChangeActivity.getStartIntent(this));
                break;
            case R.id.my_info_phone_var:
                startActivity(InfoPhoneChangeActivity.getStartIntent(this));
                break;
            case R.id.my_info_sign_var:
                startActivity(InfoSignChangeActivity.getStartIntent(this));
                break;
            case R.id.my_info_sex_var:
                startActivity(InfoSexChangeActivity.getStartIntent(this));
                break;

            default:
                break;
        }
    }

    public static Intent getStartIntent(Context context) {
        return new Intent(context, MyInfoActivity.class);
    }


}
